// KV相关的辅助函数
export const kvHelper = {
  // 设置登录challenge
  async setLoginChallenge(env, email, challenge) {
    const key = `login_challenge:${email}`;
    await env.MIMI_KV.put(key, challenge, { expirationTtl: 600 });
  },

  // 获取登录challenge
  async getLoginChallenge(env, email) {
    const key = `login_challenge:${email}`;
    return await env.MIMI_KV.get(key);
  },

  // 删除登录challenge
  async deleteLoginChallenge(env, email) {
    const key = `login_challenge:${email}`;
    await env.MIMI_KV.delete(key);
  },

  // 设置登录认证salt
  async setLoginAuthSalt(env, email, salt) {
    const key = `login_auth_salt:${email}`;
    await env.MIMI_KV.put(key, salt, { expirationTtl: 600 });
  },

  // 获取登录认证salt
  async getLoginAuthSalt(env, email) {
    const key = `login_auth_salt:${email}`;
    return await env.MIMI_KV.get(key);
  },

  // 删除登录认证salt
  async deleteLoginAuthSalt(env, email) {
    const key = `login_auth_salt:${email}`;
    await env.MIMI_KV.delete(key);
  },

  // 设置登录加密salt
  async setLoginEncryptSalt(env, email, salt) {
    const key = `login_encrypt_salt:${email}`;
    await env.MIMI_KV.put(key, salt, { expirationTtl: 600 });
  },

  // 获取登录加密salt
  async getLoginEncryptSalt(env, email) {
    const key = `login_encrypt_salt:${email}`;
    return await env.MIMI_KV.get(key);
  },

  // 删除登录加密salt
  async deleteLoginEncryptSalt(env, email) {
    const key = `login_encrypt_salt:${email}`;
    await env.MIMI_KV.delete(key);
  },

  // 设置主密钥（带过期时间）
  async setMasterKey(env, email, masterKey, expirationSeconds) {
    const key = `master_key:${email}`;
    await env.MIMI_KV.put(key, masterKey, { expirationTtl: expirationSeconds });
  },

  // 获取主密钥
  async getMasterKey(env, email) {
    const key = `master_key:${email}`;
    return await env.MIMI_KV.get(key);
  },

  // 删除主密钥
  async deleteMasterKey(env, email) {
    const key = `master_key:${email}`;
    await env.MIMI_KV.delete(key);
  }
}; 